﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eDepotSystem;

namespace eDepotSystemConsole
{
    static class Sys
    {
        static private List<eDepotSystem.Depot> depots;
        static public void Run()
        {
            throw new NotImplementedException();
        }

        /*static public Depot LocateDepot()
        {
            throw new NotImplementedException();
        }*/
        static public Depot GetDepotByName(string depotName)
        {
            Depot dep;
            if ((dep = depots.Find(d => d.GetName() == depotName)) != null)
            {
                return dep;
            };
            return null;
        }

        static public bool LogOn(Depot depot, Driver driver)
        {
            throw new NotImplementedException();
        }
        static public bool LogOff(Depot depot, Driver driver)
        {
            throw new NotImplementedException();
        }
        /*static public List<WorkSchedule> SchedulesByDepot(Depot depot)
        {
            return depot.GetSchedules();
            throw new NotImplementedException();
        }*/

        static public List<WorkSchedule> SchedulesByDriver(Driver driver)
        {
            Depot depot = GetDepotByName(driver.GetDepotName());
            return depot.GetSchedulesByDriver(driver);
        }
        static public void DisplaySchedule(WorkSchedule schedule)
        {
            DisplayMessage("Schedule for: " + schedule.GetClient());
            DisplayMessage("    Vehicle reg: " + schedule.GetVehicle().GetRegNumber());
            DisplayMessage("    Driver: " + schedule.GetVehicle().GetDriver().GetUserName());
            DateTime ttime;
            ttime = schedule.GetStartDate();
            DisplayMessage("    Starts: " + ttime.Hour + ":" + ttime.Minute + "." + ttime.Second + " " + ttime.Day + " " + ttime.Month + " " + ttime.Year);
            ttime = schedule.GetEndDate();
            DisplayMessage("    Ends: " + ttime.Hour + ":" + ttime.Minute + "." + ttime.Second + " " + ttime.Day + " " + ttime.Month + " " + ttime.Year);
        }
        static public bool MoveVehicle(string regNumber, Depot fromDepot, Depot toDepot){
            Vehicle v = fromDepot.LocateVehicle(regNumber);
            if (v != null)
            {

            }

            throw new NotImplementedException();
        }


        static public string GetInput()
        {
            return Console.ReadLine();
        }
        /*
         * Some code borrowed from:
         * http://msdn.microsoft.com/en-us/library/bb397679.aspx
         * 
         * Displays a menu and waits for input. Will repeatedly display the menu
         * until a valid input is given. Will block while this happens.
         * 
         * Will return an int of value 1 to (items length + 1).
         */
        static public short WaitForMenu(List<string> items)
        {
            bool choice_ok = false; //set true when choice is OK!
            string inp;
            short choice = -1;
            do
            {
                ResetDisplay();
                DisplayMessage(items);
                inp = GetInput();
                //An overly paranoid way of grabbing input.
                try
                {
                    choice = Convert.ToInt16(inp);
                }
                catch (FormatException e)
                {
                    DisplayMessage("Input string is not a sequence of digits.");
                }
                catch (OverflowException e)
                {
                    DisplayMessage("The number cannot fit in an Int16.");
                }
                finally
                {
                    if (choice < Int16.MaxValue)
                    {
                        DisplayMessage("The new value is " + (choice + 1));
                    }
                    else
                    {
                        DisplayMessage("choice cannot be incremented beyond its current value");
                    }
                }
                /*
                 * Now to check the input is in the range required.
                 */
                if ((choice > 0) && (choice <= items.Count())){
                    choice_ok = true;
                }

            } while (!choice_ok);
            return choice;
        }
        static public void ResetDisplay()
        {
            Console.Clear();
        }
        static public void DisplayMessage(string message)
        {
            Console.WriteLine(message);
        }
        static public void DisplayMessage(List<string> messages)
        {
            foreach (string message in messages)
            {
                DisplayMessage(message);
            }
        }

    }
}
